package com.yoda.mall.mysql.api;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yoda.mall.api.AdminApi;
import com.yoda.mall.model.entity.cms.Subject;
import com.yoda.mall.model.entity.ums.*;
import com.yoda.mall.model.vo.ManagerInfoVo;
import com.yoda.mall.model.vo.PageResult;
import com.yoda.mall.mysql.mapper.*;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author shawn
 * @date 2022年 12月 15日 21:39
 */
@DubboService
public class AdminApiImpl implements AdminApi {

    @Resource
    private AdminMapper adminMapper;
    @Resource
    private RoleMenuRelationMapper roleMenuRelationMapper;

    @Resource
    private AdminRoleRelationMapper adminRoleRelationMapper;

    @Resource
    private MenuMapper menuMapper;

    @Resource
    private RoleMapper roleMapper;
    @Override
    public Admin selectByName(String username) {
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getUsername,username);
        return adminMapper.selectOne(wrapper);
    }

    @Override
    public Admin selectById(Long adminId) {

        return adminMapper.selectById(adminId);
    }

    @Override
    public ManagerInfoVo getAdminInfo(Long adminId) {
        //查询管理员信息
        Admin admin = adminMapper.selectById(adminId);
        //1 查询角色id
        LambdaQueryWrapper<AdminRoleRelation> adminRoleWrapper = new LambdaQueryWrapper<>();
        adminRoleWrapper.eq(AdminRoleRelation::getAdminId,adminId);

        List<AdminRoleRelation> adminRoleRelations = adminRoleRelationMapper.selectList(adminRoleWrapper);

        //获取所有角色id
        List<Long> roleIds = adminRoleRelations.stream().map(AdminRoleRelation::getRoleId).collect(Collectors.toList());
        //获取所有角色名集合
        List<Role> roles = roleMapper.selectBatchIds(roleIds);
        List<String> roleNames = roles.stream().map(Role::getName).collect(Collectors.toList());
        //2 根据角色id查询对应菜单id
        LambdaQueryWrapper<RoleMenuRelation> roleMenuWrapper = new LambdaQueryWrapper<>();
        roleMenuWrapper.in(RoleMenuRelation::getRoleId,roleIds);
        List<RoleMenuRelation> roleMenuRelations = roleMenuRelationMapper.selectList(roleMenuWrapper);
        List<Long> menuIds = roleMenuRelations.stream().map(RoleMenuRelation::getMenuId).collect(Collectors.toList());
        //3 根据菜单id集合查询菜单
        List<Menu> menus = menuMapper.selectBatchIds(menuIds);
        return ManagerInfoVo.init(admin,roleNames,menus);
    }

    @Override
    public PageResult<Admin> selectByPage(Integer page, Integer pageSize, String keyword) {
        Page<Admin> accountPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(keyword)){
            wrapper.like(Admin::getUsername,keyword).or().like(Admin::getNickName,keyword);
        }
        wrapper.orderByDesc(Admin::getLoginTime);
        adminMapper.selectPage(accountPage, wrapper);
        return new PageResult<>(page, pageSize, (int) accountPage.getTotal(), accountPage.getRecords());
    }

    @Override
    public Admin registerAccount(Admin admin) {
        adminMapper.insert(admin);
        return admin;
    }

    @Override
    public Integer assigningRoles(Long adminId, List<Long> roleIds) {
        List<AdminRoleRelation> adminRoleRelations = roleIds.stream().map(id -> {
            AdminRoleRelation arr = new AdminRoleRelation();
            arr.setAdminId(adminId);
            arr.setRoleId(id);
            return arr;
        }).collect(Collectors.toList());
       return adminRoleRelationMapper.saveBatch(adminRoleRelations);
    }

    @Override
    public Integer updateAccount(Admin admin) {
       return adminMapper.updateById(admin);
    }

    @Override
    public Integer deleteAccountById(Long id) {
        return adminMapper.deleteById(id);
    }

    @Override
    public List<Role> getSpecifyRole(Long adminId) {
        //获取账户对应的角色id
        LambdaQueryWrapper<AdminRoleRelation> roleRelationWrapper = new LambdaQueryWrapper<>();
        roleRelationWrapper.eq(AdminRoleRelation::getAdminId,adminId);
        List<AdminRoleRelation> adminRoleRelations = adminRoleRelationMapper.selectList(roleRelationWrapper);
        if (Objects.isNull(adminRoleRelations)||adminRoleRelations.size()==0){
            return new ArrayList<>();
        }
        List<Long> roleIds = adminRoleRelations.stream().map(AdminRoleRelation::getRoleId).collect(Collectors.toList());
        //根据角色id查询对应角色
        LambdaQueryWrapper<Role> roleWrapper = new LambdaQueryWrapper<>();
        roleWrapper.in(Role::getId,roleIds);
        return roleMapper.selectList(roleWrapper);
    }
}
